import LogUtil from "./LogUtil";

/**
 * 对象工具
 * @see ObjectUtil.ts 
 */
export default class ObjectUtil {

    /**
     * 判断指定的值是否为对象
     * @param value 值
     */
    public static isObject(value: any): boolean {
        var type = Object.prototype.toString.call(value);
        return type === '[object Object]' || type === '[object Array]';
    }
    
    /**
     * 判断对象是否为空
     * @param obj 
     * @returns 
     */
    public static isNull(obj: object): boolean {
        let result = true;
        if (obj) {
            for (const key in obj) {
                result = false;
                break;
            }
        }
        return result;
    }

    public static isNumber(value: any): boolean {
        return typeof (value) == 'number';
    }

    /**
     * 深拷贝
     * @param target 目标
     */
    public static deepCopy(target: any): any {
        if (target == null || typeof target !== 'object') {
            return target;
        }

        let result = null;

        if (target instanceof Date) {
            result = new Date();
            result.setTime(target.getTime());
            return result;
        }

        if (target instanceof Array) {
            result = [];
            for (let i = 0, length = target.length; i < length; i++) {
                result[i] = this.deepCopy(target[i]);
            }
            return result;
        }

        if (target instanceof Object) {
            result = {};
            for (const key in target) {
                if (target.hasOwnProperty(key)) {
                    result[key] = this.deepCopy(target[key]);
                }
            }
            return result;
        }

        LogUtil.warn(`不支持的类型：${result}`);
    }

    /**
     * 拷贝对象
     * @param target 目标
     */
    public static copy(target: object): object {
        return JSON.parse(JSON.stringify(target));
    }

    /**
     * 拷贝对象
     * @param target 目标
     */
    // public static copy2(target: object): object {
    //     return {...target};
    // }

    /**
     * 合并对象
     * @param target1 
     * @param target2 
     * @returns 
     */
    // public static merge(target1,target2): object {
    //     return {...target1,...target2};
    // }

    /**
     * 对象转数组
     * @param target 目标
     */
    public static objConvertArray(target: object): any[]{
        let list:any[] = [];
        for(let k in target){
            list.push(target[k]);
        }
        return list;
    }

}
